/**
 * Created by zhanglihe on 15/11/7.
 */
require.config(require_config);

define([
    'jquery',
    'common',
    'ready',
    'collection',
    'template',
    'model',
    'dialog',
    'datepicker'
], function ($, common, ready, Collection, template, Model, dialog, datepicker) {
    var index = function () {
        this.init();
    };
    index.prototype = {
        init: function () {
            var _this = this;
            loading();
            this.fetch();
            this.event();
        },
        fetch: function () {
            var _this = this;
            var model = new Model({
                container: 'main-view',
                template: 'edit-tpl',
                tag_name: 'div',
                url: _global.url.api + 'coupon',
                data: {id: id},
                callback: function () {
                    loaded();
                    console.log(model);
                    if (model.data.goods.length > 0) {
                        $("#div_content_goods").show();
                        for (var i = 0; i < model.data.goods.length; i++) {
                            var data = model.data.goods[i];
                            $('#goods-item').append(template('goods-item-tpl', {
                                goods_id: data.id,
                                goods_name: data.name,
                                price: data.price
                            }));
                        }
                        $('#div_goods').show();
                        $('.goods-item-del').on('click', function (e) {
                            e.preventDefault();
                            $(this).parent().parent().remove();
                        });
                    } else {
                        $("#div_content_goods").hide();
                    }
                }
            });
            model.fetch();

            $("#type-content").html(template('data-type-1'));
        },
        event: function () {
            this.data = {};
            var _this = this;

            $("input[name='is_goods']").on("click", function (e) {
                if ($(this).val() == 1) {
                    $("#div_content_goods").show();
                } else {
                    $("#div_content_goods").hide();
                }
            })
            $("#btn-search").on("click", function (e) {
                e.preventDefault();
                _this.select_goods(e);
            });
            $("#type").on("change", function (e) {
                var type = $(this).val();
                if (type == 1 || type == 2) {
                    $("#type-content").html(template('data-type-' + type));
                }
            });
            //保存
            $("#btn_save").on("click", function () {
                _this.data.id = id;
                _this.data.is_goods = $("input[name='is_goods']:checked").val();
                if (_this.data.is_goods == 1) {
                    var goods = new Array();
                    $(".goods_id").each(function () {
                        if ($(this).val()) {
                            goods.push($(this).val());
                        }
                    });
                    if (goods == '') {
                        Alert({msg: '请选择要关联的商品', type: 'error'});
                        return false;
                    }
                    _this.data.goods = goods.join(',');
                }
                if (new Model({
                        url: _global.url.api + 'coupon',
                        data: _this.data
                    }).put()) {
                    location.href = '/site/coupon';
                }

            });
        },

        change: function (e) {
            this.data[$(e.target).attr('name')] = $(e.target).val();
        },
        select_goods: function (e) {
            this.collection = new Collection({
                container: 'data-list',
                template: 'data-items',
                url: _global.url.api + 'promotion_goods_search',
                condition: {keyword: $("#keyword").val()},
                model: {
                    event: {
                        '.item-bind:click': 'addgoods',
                        '.show-online:click': 'setshow'
                    },
                    remove: function (e) {
                        e.preventDefault();
                        var model = this;
                        dialog({
                            'content': '确定删除吗?',
                            ok: function () {
                                if (model.del()) {
                                    _this.collection.reload();
                                }
                                ;
                            }, cancel: function () {
                            }
                        }).show(e.target);
                    },
                    addgoods: function (e) {
                        e.preventDefault();
                        var _this = this;
                        var id = this.data.id;
                        var vals = new Array();
                        $(".goods_id").each(function () {
                            if ($(this).val()) {
                                vals.push($(this).val());
                            }
                        });
                        var goods_id = ',' + vals.join(",") + ',';
                        if (goods_id.indexOf(',' + id + ',') != -1) {
                            Alert({msg: '请不要重复添加同一商品', type: 'error'});
                            return false;
                        }
                        $('#goods-item').append(template('goods-item-tpl', {
                            goods_id: this.data.id,
                            goods_name: this.data.name
                        }));
                        $("#div_goods").show();
                        $('.goods-item-del').on('click', function (e) {
                            e.preventDefault();
                            $(this).parent().parent().remove();
                            $('.item-bind' + id).html('选择').removeClass('btn-success').addClass('btn-default');
                        });

                        $('.item-bind' + id).html('已选择').removeClass('btn-default').addClass('btn-success');
                    }

                },
                callback: function () {
                    loaded();
                }
            });
            this.collection.fetch();
        }

    };

    new index();

});